"""Configuration management for ChatWithRepository."""

import os
from pathlib import Path
from typing import Optional
from dotenv import load_dotenv

load_dotenv()

class Config:
    """Application configuration."""
    
    # API Keys
    OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY", "")
    GITHUB_TOKEN: str = os.getenv("GITHUB_TOKEN", "")
    
    # Vector database
    CHROMA_PERSIST_DIR: str = os.getenv("CHROMA_PERSIST_DIR", "./chroma_db")
    
    # Repository storage
    REPOS_DIR: str = os.getenv("REPOS_DIR", "./repos")
    
    # Model configuration
    EMBEDDING_MODEL: str = os.getenv("EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
    CHAT_MODEL: str = os.getenv("CHAT_MODEL", "kimi-k2-0711-preview")
    
    # KIMI API Configuration
    KIMI_API_KEY: str = os.getenv("KIMI_API_KEY", "")
    KIMI_API_BASE: str = os.getenv("KIMI_API_BASE", "https://api.moonshot.cn/v1")
    
    # Chunking configuration
    CHUNK_SIZE: int = int(os.getenv("CHUNK_SIZE", "1000"))
    CHUNK_OVERLAP: int = int(os.getenv("CHUNK_OVERLAP", "200"))
    
    # Processing configuration
    MAX_FILE_SIZE: int = int(os.getenv("MAX_FILE_SIZE", "1048576"))  # 1MB
    SUPPORTED_EXTENSIONS: list[str] = [
        '.py', '.js', '.ts', '.jsx', '.tsx', '.java', '.go', 
        '.rs', '.cpp', '.c', '.h', '.hpp', '.cs', '.php', '.rb'
    ]
    
    @classmethod
    def validate(cls) -> bool:
        """Validate configuration settings."""
        if not cls.KIMI_API_KEY:
            raise ValueError("KIMI_API_KEY environment variable is required")
        return True
    
    @classmethod
    def get_repo_path(cls, repo_url: str) -> Path:
        """Get local path for a repository."""
        repo_name = repo_url.split('/')[-1].replace('.git', '')
        return Path(cls.REPOS_DIR) / repo_name